package cn.tx.controller;


import cn.tx.model.Course1;
import cn.tx.page.Page;
import cn.tx.service.Course1Service;
import cn.tx.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static cn.tx.model.Course1.*;

@Controller
@RequestMapping(path = "/course1")

public class Course1Controller {
    @Autowired

    private Course1Service course1Service;

    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("course1/course1_list");
        return modelAndView;
    }


    /*展示课程列表*/
    @RequestMapping(path = "/course1_list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(
            @RequestParam(value="cname",required=false,defaultValue="") String cname,
            Page page
    ) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        querymap.put("cname","%"+cname+"%");
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows",course1Service.findList(querymap));
        map.put("total",course1Service.findTotal(querymap));
        return map;
    }



    /*添加课程*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Course1 course1) {
        Map<String, String> map = new HashMap<>();


        if (course1 == null) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        if (StringUtils.isEmpty(course1.getCname())) {
            map.put("type", "error");
            map.put("msg", "课程名不能为空");
            return map;
        }
        if(course1.getCdatestr()!=null){
            try{
                SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd");
                if (!StringUtils.isEmpty(course1.getCdatestr())) {
                    course1.setCdate(lsdStrFormat.parse(course1.getCdatestr()));
                }
            }catch(Exception e){}
        }



        if (StringUtils.isEmpty(course1.getTname())) {
            map.put("type", "error");
            map.put("msg", "备注不能为空");
            return map;
        }


        if (course1Service.addCourse1(course1) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }


    /*编辑课程*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
        public Map<String, String> edit(Course1 course1) {
        Map<String, String> map = new HashMap<>();

        System.out.println(course1);
        if(course1.getCdatestr()!=null){
            try{
                SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd");
                if (!StringUtils.isEmpty(course1.getCdatestr())) {
                    course1.setCdate(lsdStrFormat.parse(course1.getCdatestr()));
                }
            }catch(Exception e){}
        }


        if (course1Service.UpdateCourse1ById(course1)<= 0) {
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功");
        return map;
    }


    /*删除课程*/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "ids[]",required = true) Long[] ids
    )


    {
        Map<String, String> map = new HashMap<>();
        if (ids==null||ids.length==0){
            map.put("type", "error");
            map.put("msg", "请至少选择一条数据");
            return map;
        }
        try {
            if(course1Service.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                map.put("type", "error");
                map.put("msg", "删除失败！");
                return map;
            }
        } catch (Exception e) {
            map.put("type", "error");
            map.put("msg", "该年级下存在班级信息，请勿冲动！");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功");
        return map;
    }



}
